package jfilters.filters.composite;

/*
    Copyright Janne Liljeblad 2008.

    This file is part of JFilters.

    JFilters is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    JFilters is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with JFilters.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.awt.image.BufferedImage;
import jfilters.filters.JFilter;
import jfilters.libcolor.*;

//--- gimp file: /app/composite/gimp-composite-generic.c
public class JFSaturationComposite extends JFCompositeFilter
{

	public BufferedImage filter( BufferedImage srcImg, BufferedImage destImg )
	{
		int da;	

		int[] src = getBank( srcImg );
		int[] dest = getBank( destImg );

		JFRGBInt srgb = new JFRGBInt();
		JFRGBInt drgb = new JFRGBInt();
		JFHSVInt shsv = new JFHSVInt();
		JFHSVInt dhsv = new JFHSVInt();

		for( int i = 0; i < src.length; i++ )
		{
			srgb.r = ( src[ i ] >> red ) & 0xff;
			srgb.g = ( src[ i ] >> green ) & 0xff;
			srgb.b = src[ i ] & 0xff;

			da = ( dest[ i ] >> alpha ) & 0xff;
			drgb.r = ( dest[ i ] >> red ) & 0xff;
			drgb.g = ( dest[ i ] >> green ) & 0xff;
			drgb.b = dest[ i ] & 0xff;

			dr = drgb.r;
			dg = drgb.g;
			db = drgb.b;

        		JFColorSpace.jimp_rgb_to_hsv_int( srgb, shsv );
        		JFColorSpace.jimp_rgb_to_hsv_int( drgb, dhsv );

       			dhsv.s = shsv.s;

			JFColorSpace.jimp_hsv_to_rgb_int( dhsv, drgb );

			//-- This is to set up doOpacity
			sr = drgb.r;
			sg = drgb.g;
			sb = drgb.b;

			doSourceOpacity();

			dest[ i ] = da << alpha | dr << red | dg << green | db;
		}
		return destImg;
	}

}//end  class